package tools

import (
	"fmt"
	"reflect"
)

func C2UpdatesMap(s interface{}, u interface{}) map[string]interface{} {
	if s != nil && u != nil {
		so := reflect.ValueOf(s)
		sr := so.Elem()
		//uo := reflect.ValueOf(u)
		//ur := uo.Elem()
		st := sr.Type()

		data := make(map[string]interface{})
		for i := 0; i < sr.NumField(); i++ {
			sf := sr.Field(i)
			uf := reflect.ValueOf(u).Field(i)
			if sf.Interface() != uf.Interface() {
				data[st.Field(i).Name] = sf.Interface()
			}
			//fmt.Printf("%d. %s %s = %v \n", i, st.Field(i).Name, sf.Type(), sf.Interface())
		}
		//fmt.Println()
		//fmt.Println(data)
		return data
		//tonydon.SayHello()
		//v := object.MethodByName("SayHello")
		//v.Call([]reflect.Value{})
	}
	return nil
}

func ToUpdatesMap(s interface{}, col []string) map[string]interface{} {
	fmt.Println(s)
	fmt.Println(col)
	if s != nil && len(col) > 0 {
		data := make(map[string]interface{})
		for _, c := range col {
			so := reflect.ValueOf(s)
			sr := so.Elem()
			st := sr.Type()
			for i := 0; i < sr.NumField(); i++ {
				if st.Field(i).Name == c {
					data[c] = sr.Field(i).Interface()
				}
			}
		}
		return data
	}
	return nil
}
